#include <iostream>
#include <fstream>
#include "contacts.pb.h"

void AddContactsPeople(contacts::PeopleInfo* peopleinfo)
{
    std::cout << "--------------- 新建联系人 ---------------" << std::endl;
    std::cout << "请输入姓名：" << std::endl;
    std::string name;
    getline(std::cin,name);
    peopleinfo->set_name(name);

    std::cout << "请输入年龄：" << std::endl;
    int age;
    std::cin >> age;
    peopleinfo->set_age(age);
    // 清理缓冲区，直到\n或者清理了256个字符
    std::cin.ignore(256,'\n');
    
    for(int i = 0;;i++)
    {
        std::cout << "请输入联系人电话" << i+1 << "(输入回车完成新增): " << std::endl;
        std::string number;
        getline(std::cin,number);
        
        if(number.empty())
            break;

        std::cout << "请输入该电话类型(0. 移动电话 1. 固定电话): " << std::endl;
        int phonetype = 0;
        std::cin >> phonetype;
        std::cin.ignore(256,'\n');

        contacts::PeopleInfo_Phone* phone = peopleinfo->add_phone();
        phone->set_number(number);

        switch (phonetype)
        {
        case 0:
            phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
            break;
        case 1:
            phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
            break;

        default:
            std::cout << "非法使用, 使用默认值" << std::endl;
            break;
        }
    }

    contacts::Address address;
    std::cout << "请输入家庭地址: " << std::endl;
    std::string home;
    std::getline(std::cin,home);
    address.set_home_address(home);
    std::cout << "请输入单位地址: " << std::endl;
    std::string unit;
    std::getline(std::cin,unit);
    address.set_unit_address(unit);
    // mutable_address开辟空间给其使用
    google::protobuf::Any* data = peopleinfo->mutable_address();
    data->PackFrom(address);

    std::cout << "选择添加一个其它联系方式: 1、QQ       2、WeChat" << std::endl;
    int one_other;
    std::cin >> one_other;
    std::cin.ignore(256,'\n');
    if(1 == one_other){
        std::cout << "请输入你的QQ号码: " << std::endl;
        std::string qq;
        // getline: 此时缓冲区将\n前面的数据删除后会残留\n，而getline遇到\n会退出，所以需要ignore清空缓冲区并等待数据再次传入
        std::getline(std::cin,qq);
        peopleinfo->set_qq(qq);
    }
    else if(2 == one_other){
        std::cout << "请输入你的WeChat号码: " << std::endl;
        std::string wechat;
        std::getline(std::cin,wechat);
        peopleinfo->set_wechat(wechat);
    }
    else{
        std::cout << "选择错误, 其它联系方式添加失败! " << std::endl; 
    }

    for(int i = 0;;i++)
    {
        std::cout << "请输入备注" << i+1 << "标题(只输入回车完成备注新增)" << std::endl;
        std::string remark_key;
        std::getline(std::cin,remark_key);
        if(remark_key.empty())
            break;

        std::cout << "请输入备注" << i+1 << "内容" << std::endl;
        std::string remark_value;
        std::getline(std::cin,remark_value);
        peopleinfo->mutable_remark()->insert({remark_key,remark_value});
    }

    std::cout << "--------------- 联系人成功 ---------------" << std::endl;
}

int main()
{
    // 读取文件中的二进制数据
    contacts::Contacts contact;
    std::fstream input("contacts.bin",std::ios::in | std::ios::binary);
    if(!input)
    {
        std::cout << "contacts.bin not find, create new file!" << std::endl;
    }
    // ParseFromIstream：读取文件，并从输入流序列化为protobuf数据
    else if(!contact.ParseFromIstream(&input))
    {
        std::cerr << "Data Parse Fail" << std::endl;
        input.close();
        return -1;
    }

    // 添加通讯录联系人
    AddContactsPeople(contact.add_contacts());

    // 将数据写入文件
    std::fstream output("contacts.bin",std::ios::out | std::ios::trunc | std::ios::binary);
    if(!contact.SerializeToOstream(&output))
    {
        std::cerr << "write error!" << std::endl;
        input.close();
        output.close();
        return -1;
    }

    std::cout << "write success!" << std::endl;
    input.close();
    output.close();

    return 0;
}


